﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace BSC_OTC_Management.Forms.SoTraiChu
{
    public partial class Frm_TaoSoTraiChuTheoDanhSach : Form
    {
        Code.CL_Ultility _cl = new Code.CL_Ultility();
        DataTable dataTable = new DataTable(); //bang du lieu import
        string formName = ""; //ten bang du lieu truy van

        #region event
        public Frm_TaoSoTraiChuTheoDanhSach()
        {
            InitializeComponent();
        }

        //open file dialog
        private void btt_Select_Click(object sender, EventArgs e)
        {
            txt_path.Text = _cl.OpenFileDialog_ReturnFileName();
        }

        private void Frm_TaoSoTraiChuTheoDanhSach_Load(object sender, EventArgs e)
        {
            lb_title.Text = "1. Lựa chọn import danh sách khách hàng, sau đó kết xuất lấy mã khách hàng\r\n"+
                            "2. Lựa chọn import danh sách sổ trái chủ\r\n"+
                            "3. Lựa chọn import danh sách tài khoản\r\n"+
                            "4. Lựa chọn import danh sách tài khoản";

        }

        private void btn_Exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_Loaddata_Click(object sender, EventArgs e)
        {
            try
            {
                dataTable = _cl.CreateDataTableForExcelData(txt_path.Text);
                this.dataTable.TableName = "dataTable";
                this.dgvResult.DataSource = dataTable;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btn_Execute_Click(object sender, EventArgs e)
        {
            //bien tra ve kq
            DataTable result = new DataTable("result");
            try
            {
                switch (cbb_file.SelectedIndex)
                {
                    case 0:
                        formName = "BSC_TBL_KHACHHANG";
                        break;
                    case 1:
                        formName = "BSC_TBL_SOTRAICHU";
                        break;
                    case 2:
                        formName = "BSC_TBL_TAIKHOAN";
                        break;
                    case 3:
                        formName = "BSC_TBL_TPPH";
                        break;
                    case 4:
                        formName = "BSC_TBL_TCPH";
                        break;
                    case 5:
                        formName = "BSC_TBL_HOPDONGDAILY";
                        break;
                    default:
                        throw new Exception("Không có chức năng import này!");
                }

                result = _cl._services.Import_TableData(formName,
                                                        dataTable,
                                                        Code.StaticVariable.userID,
                                                        Code.StaticVariable.menu_Code,
                                                        (int)Code.StaticVariable.approveCase.ChuaDuyet + "",
                                                        Code.StaticVariable.function_AddNew);
                if (result != null)
                {
                    
                    MessageBox.Show("Import thành công", Code.StaticVariable.messeseboxLuuY);
                    this.dgvResult.DataSource = result;
                }
                else
                    MessageBox.Show("Import không thành công.\r\nHãy kiểm tra lại dữ liệu.", Code.StaticVariable.messeseboxLuuY);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Code.StaticVariable.messeseboxLuuY);
            }
        }

        private void btn_Export_Click(object sender, EventArgs e)
        {
            if (this.dgvResult.RowCount > 0)
            {
                Code.ExportUtility _ExportExcel = new Code.ExportUtility();
                _ExportExcel.Export(this.dgvResult);
            }
        }

        #endregion event

        
        
    }
}
